<?php
/**
 * This file is part of PHPlaza (http://code.google.com/p/phplaza)
 * This file is licensed under the GNU Lesser General Public License
 *      http://www.gnu.org/licenses/lgpl.html
 *
 * No warranty is given, express or implied. Use at your own risk.
 */
 
namespace core::collections;
class CList implements IList
{
	private $items;
	
	public function __construct() {
		$this->items = array();
	}
	
	public function add($object)
	{
		$this->items[] = $object;
	}
	
	public function remove($object)
	{
		foreach($this->items as $key => $value)
		{
			if ($key === $object)
			{
				unset($this->items[$key]);
			}
		}
	}
	
	public function clear()
	{
		$this->items = array();
	}

	public function offsetExists($offset)
	{
		return isset($this->items[$offset]);
	}
	
	public function offsetGet($offset)
	{
		if (!isset($this->items[$offset]))
		{
			debug_print_backtrace();
		}
		return $this->items[$offset];
	}
	
	public function offsetSet($offset, $value)
	{
		$this->items[$offset] = $value;
	}
	
	public function offsetUnset($offset)
	{
		unset($this->items[$offset]);
	}
	
	public function count()
	{
		return count($this->items);
	}
	
	public function getIterator()
	{
		return new ArrayIterator($this->items);
	}
	
	protected function setItems($items)
	{
		$this->items = $items;
	}
	
	public function merge(IList $other)
	{
		foreach($other as $key => $value)
		{
			if (!isset($this->items[$key]))
			{
				$this->items[$key] = $value;
			}
		}
	}
}